
from subsidy.app.mapper.UserMapper import UserMapper
from subsidy.app.utils.JwtUtils import *
from subsidy.app.flashClass.app.flashClass import FlashClass

class UserServiceImpl(UserMapper):
    def __init__(self):
       super().__init__()

    def login(self, userInfo) -> dict:
        """用户登录"""
        # 如果用户没有注册则帮忙注册再进行登录
        exitUser = self.fondUserExit(userInfo)
        if exitUser:
            """没有用户注册"""
            return {"status":301,"message": "未找到用户", "data": {"token": None}}
        else:
            return self.checklogin(userInfo)

    def register(self, data) -> dict:
        """一键登录"""
        userInfo = {
            "username": data['username'],
            "password": data['password'],
        }
        # 如果用户没有注册则帮忙注册再进行登录
        exitUser = self.fondUserExit(userInfo)
        if(exitUser):
            """没有用户注册"""
            isRegister = self.registerUser(userInfo)
            if isRegister:
                # 注册成功 进行登录
               return {"status":200,"message": "用户注册成功"}
            else:
                # 注册失败
                return {"status":301,"message": "用户注册失败!请联系管理员"}
        else:
            return {"status":200,"message": "用户已经存在"}

    def addUser(self, data) -> dict:
        """一键登录"""
        userInfo = {
            "username": data['username'],
            "password": data['password'],
        }
        # 如果用户没有注册则帮忙注册再进行登录
        exitUser = self.fondUserExit(userInfo)
        if(exitUser):
            """没有用户注册"""
            isRegister = self.registerUser(userInfo)
            if isRegister:
                # 注册成功 进行登录
               return self.checklogin(userInfo)
            else:
                # 注册失败
                return {"status":301,"message": "用户注册失败!请联系管理员", "data": {"token": None}}
        else:
            return self.checklogin(userInfo)



    def getAllClassinfo(self, data):
        """获取全部课程列表"""
        datas = self.querAllClassInfo(data.get("idCar"))
        return {"status": 200, "message": "操作成功", "data": {"list": datas}}

    def parseJwt(self, token):
        isUser, info = parse_jwt(token)
        if isUser:
            return {"status": 200, "message": "操作成功", "data": {"user": info}}
        return {"status": 401, "message": "操作失败, 请重新登录", "data": None}

    def setIdentity(self, data):
        ident = self.quryIdentityMapper(data)
        if ident is None:
            self.setIdentityMapper(data)
            return {"status": 200, "message": "账号验证成功"}
        return {"status": 301, "message": "账号已被绑定,请先解绑"}



    def delIdentityService(self, data):
        d = self.delIdentityMapper(data)
        if d:
            return {"status": 200, "message": "绑定信息删除成功"}
        return {"status": 301, "message": "绑定信息删除失败"}

    def flashClassService(self, data):
        f = FlashClass(data.get("idCar"), data.get("password"), data.get("id"))
        f.session.headers.clear()
        key = f.getPublicKey()
        f.getLoginToken(key)
        f.updateAccessToken()
        classInfo = f.getStudentStudy()
        f.getAllcoursesPreview(classInfo)
        f.updateProgress()


